﻿@using Fierce.MvcExtend.Controls.Manage

<div class="easyui-layout" data-options="fit:true">

    <div data-options="region:'north',border:false,height:112">
        <div class="document-name">@ViewBag.Work.Menu.Name</div>
        <div class="document-toolbar">@Easyui.Toolbar(ViewBag.Work)</div>
    </div>

    <div data-options="region:'center',border:false"><table id="igrid" /></div>

</div>

<script type="text/javascript">
    $(function () {
        //表格初始化
        var gridInit = function () {
            $("#igrid").datagrid({
                url: "/basic/role/grid",
                nowrap: false,
                rownumbers: true,
                fit: true,
                singleSelect: true,
                idField: "ID",
                pageSize: 20,
                pagination: true,
                columns: [[
                    { field: "Name", title: "名称", width: 120, align: "center" },
                    { field: "Remark", title: "描述", width: 280, align: "center" },
                    { field: "Sequence", title: "排序", width: 50, align: "center" },
                    {
                        field: "IsEnabled", title: "启用", width: 50, align: "center",
                        formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                    },
                    { field: "InsertTime", title: "添加时间", width: 140, align: "center" }
                ]]
            });
        }();
        //添加
        var insert = function () {
            mutual("添加", null);
        };
        //修改
        var update = function () {
            var row = $("#igrid").datagrid("getSelected");
            if (row) {
                mutual("修改", row.ID);
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //删除
        var remove = function () {
            var row = $("#igrid").datagrid("getSelected");
            if (row) {
                manage.confirm("确认删除?", function (del) {
                    if (del) {
                        manage.ajax({
                            url: "/basic/role/remove",
                            type: "post", data: { id: row.ID },
                            success: function (result) {
                                manage.slide(result.message);
                                if (result.success) { reload(); }
                            }
                        });
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //实体操作
        var mutual = function (title, id) {
            var href = "/basic/role/page?" + $.param({ id: id });
            var d = manage.dialog({
                title: title, href: href, width: 420,
                bodyCls: "theme-container",
                handler: function () {
                    manage.form({
                        dialog: d, formid: "#dform",
                        url: "/basic/role/save",
                        success: function (result) { reload(); }
                    });
                },
                onLoad: function () { formInit(); }
            });
        };
        //弹窗初始化
        var formInit = function () {

        };
        //排序上移
        var desc = function () {
            order("desc");
        };
        //排序下移
        var asc = function () {
            order("asc");
        };
        //排序操作
        var order = function (order) {
            var row = $("#igrid").datagrid("getSelected");
            if (row) {
                manage.ajax({
                    url: "/basic/role/setorder",
                    type: "post", data: { id: row.ID, order: order },
                    success: function (result) {
                        manage.slide(result.message);
                        if (result.success) {
                            $("#igrid").datagrid("reload");
                        }
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //启用
        var enable = function () {
            enableOrDisable(true);
        };
        //禁用
        var disable = function () {
            enableOrDisable(false);
        };
        //启用禁用
        var enableOrDisable = function (bool) {
            var row = $("#igrid").datagrid("getSelected");
            if (row) {
                manage.ajax({
                    url: "/basic/role/enableordisable",
                    type: "post", data: { id: row.ID, bit: bool },
                    success: function (result) {
                        manage.slide(result.message);
                        if (result.success) {
                            $("#igrid").datagrid("reload");
                        }
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //刷新列表
        var reload = function () {
            $("#igrid").datagrid("unselectAll");
            $("#igrid").datagrid("reload");
        };
        //权限
        var purview = function () {
            var row = $("#igrid").datagrid("getSelected");
            if (row) {
                var d = manage.dialog({
                    title: "分配权限",
                    width: 1020,
                    height: $(window).height() - 20,
                    href: "/basic/role/purview",
                    toolbar: purviewTools(),
                    handler: function () {
                        manage.ajax({
                            url: "/basic/role/setpurview", type: "post",
                            data: { id: row.ID, data: purviewData() },
                            success: function (result) {
                                if (result.success) {
                                    d.dialog("close");
                                    manage.slide(result.message);
                                }
                                else { manage.msg(result.message); }
                            }
                        });
                    },
                    onLoad: function () { purviewInit(row.ID); }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //权限工具栏
        var purviewTools = function () {
            return [{
                iconCls: 'icon-edit-add', text: "全选", handler: function () {
                    $("input[type='checkbox']", "#purviewbox").prop("checked", true);
                }
            }, {
                iconCls: 'icon-remove', text: "取消", handler: function () {
                    $("input[type='checkbox']", "#purviewbox").prop("checked", false);
                }
            }, {
                iconCls: 'icon-undo', text: "折叠", handler: function () {
                    $("#purviewgrid").treegrid("collapseAll");
                }
            }, {
                iconCls: 'icon-redo', text: "展开", handler: function () {
                    $("#purviewgrid").treegrid("expandAll");
                }
            }];
        };
        //权限表格初始化
        var purviewInit = function (id) {
            $("#purviewgrid").treegrid({
                title: "授权信息",
                url: "/basic/role/purviewgrid?" + $.param({ id: id }),
                rownumbers: true,
                fit: true,
                singleSelect: true,
                idField: "ID",
                treeField: "Name",
                parentField: "Pid",
                columns: [[
                    { field: "Name", title: "名称", width: 200, align: "left" },
                    {
                        field: "PurviewList", title: "权限", width: 610, align: "left",
                        formatter: function (value, row, index) {
                            if (value) {
                                var html = "<div id='purviewbox-" + row.ID + "' class='purviewbox'>";
                                for (var i = 0; i < value.length; i++) {
                                    if (value[i].exist) {
                                        html += "<input mfid='" + value[i].ID + "' type='checkbox' checked='checked' />";
                                    }
                                    else {
                                        html += "<input mfid='" + value[i].ID + "' type='checkbox' />";
                                    }
                                    html += "<label>" + value[i].Name + "</label>";
                                }
                                return html + "</div>";
                            }
                        }
                    },
                    {
                        field: "Opt", title: "整行操作", width: 80, align: "center",
                        formatter: function (value, row, index) {
                            return manage.iconNoTextButton("icon-key-add", "全选", "basicrolepurview", "purviewCheckRow", { id: row.ID }) +
                                manage.iconNoTextButton("icon-key-delete", "清除", "basicrolepurview", "purviewUnCheckRow", { id: row.ID });
                        }
                    }
                ]],
                onLoadSuccess: function () { manage.buttonInit("basicrolepurview", function (f) { return eval(f); }); }
            });
        };
        //权限数据
        var purviewData = function () {
            var cheks = $("input:checked", "#purviewbox");
            var data = [];
            for (var i = 0; i < cheks.length; i++) {
                data.push($(cheks[i]).attr("mfid"));
            }
            return JSON.stringify(data);
        };
        //权限行全选
        var purviewCheckRow = function () {
            var id = $(this).data("id");
            $("input[type='checkbox']", "#purviewbox-" + id).prop("checked", true);
        };
        //权限取消全取消
        var purviewUnCheckRow = function () {
            var id = $(this).data("id");
            $("input[type='checkbox']", "#purviewbox-" + id).prop("checked", false);
        };
        //按钮事件
        manage.buttonInit("basicrole", function (f) { return eval(f); });
    });
</script>